package custom;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class User {
	
	private String name;
	private int id;
	private boolean isAdmin = false;
	
	//gadmoecema saxeli...
	public User(String name) throws SQLException{
		this.name = name;
	/*	ResultSet rs = QuizDBC.getInstance().selectUser(-1, name, "");
		rs.next();
		id = rs.getInt("userid");
	*/	id=7;
		isAdmin=true;
	}
	

	/**
	 * gveubneba mocemuli useris saxeli aris tu ara name
	 * @param id
	 * @return
	 */
	public boolean areYou(int id){
		return this.id==id;
	}
	
	//gveubneba mocemuli useri aris tu ara admini
	public boolean areYouAdmin(){
		return isAdmin;
	}
	
	public int getId(){
		return id;
	}

	public String getName(){
		return name;
	}
	//gveubneba megobrebshi yavs tu ara mocemuli saxeli
	public boolean isFriendWith(int id ){
		//bazebis klasshi metodi: 2 kacis id  gadaecema da abrunebs aris tu ara megobrebi
		
		return true;
	}
	
	//gveubneba sheqmnili aqvs tu ara am saxelis quizi
	public boolean isOwnerOf(int qzId){
		return true;
	}
	
	//abrunebs suratis da saxeli html-s
	public static String getPictureNameHtml(int id){
		return "";
	}
	
	//abrunebs saxelis html-s
	public static String getNameHtml(int id){
		return "Nika Machika";
	}
	
	//abrunebs ra chesti aqvs... ticxvit
	public static int getStatus(int id) throws SQLException{
		//bazidan vigeb scores da aqedan vabruneb statuss

		ResultSet rs = QuizDBC.getInstance().selectUser(id, "", "");
		rs.next();
		int k = rs.getInt("score");
		return k/10;
	}
	

	/**
	 * abrunebs megobrebis id-ebis vectors
	 * @return
	 * @throws SQLException 
	 */
	public ArrayList<Integer> getFriends() throws SQLException{
		ArrayList<Integer> list = new ArrayList<Integer>();
		ResultSet rs = QuizDBC.getInstance().selectFromFriendsTable(id, -1);
		int a,b;
		while(rs.next()){
			a = rs.getInt("firstfriend");
			b = rs.getInt("secondid");
			if(id == b){
				list.add(a);
			} else {
				list.add(b);
			}
		}
		return list;
	}
	public ArrayList<Activity> getFriendsActivities(){
		return new ArrayList<Activity>();
	}

	public static ArrayList<Activity> getActivitiesOf(int id){
		return new ArrayList<Activity>();
	}
	
	//abrunebs useris sheqmnili quizebis veqtors
	public ArrayList<Quiz> createdQuizes(){
		return null;
	}
	
	//bazashi wers ro quizi aigo  
	public void tookQuiz(int qzId, int percent){
		//bazashi amatebs
	}
	
	//ramdeni qula unda moematos imas gadmocem
	public void updateScore(int score){
		
	}

	public static String getName(int id){
		return "Dzamuka";
	}
	
	public static void changeAdminStatus(int userId){
		
	}
	
	public static User register(String name, String password){
		//passwordis hashs avigeb da bazashi davamateb
		//tan daabrunos userobieqti
		return null;
	}
	
	public static boolean checkUser(String name, String password){
		
		return true;
	}
	
	public static int getId(String name){
		return 5;
	}
	
	public static ArrayList<Integer> getAllUsers(){
		return new ArrayList<Integer>();
	}
	
	private String hashCode(String pass) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("SHA");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		md.update(pass.getBytes());
		byte[] bytes = md.digest();
		
		StringBuffer buff = new StringBuffer();
		for (int i=0; i<bytes.length; i++) {
			int val = bytes[i];
			val = val & 0xff;  // remove higher bits, sign
			if (val<16) buff.append('0'); // leading 0
			buff.append(Integer.toString(val, 16));
		}
		
		return buff.toString();
	}

	public static boolean isAdmin(int id){
		return false;
	}
	
}
	
